POJ-2253 Frogger(建图+最短路变形or最小生成树)

描述

传送门:POJ-2253 Frogger

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.

You are given the coordinates of Freddy’s stone, Fiona’s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy’s and Fiona’s stone.

输入描述

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.

输出描述

For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

示例

输入

1
2
3
4
5
6
7
8
9
10
2
0 0
3 4

3
17 4
19 4
18 5

0

输出

1
2
3
4
5
Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

题解

题目大意

青蛙想从一个石子跳到另一个石子,中间有很多石子,要求青蛙跳跃距离应为路径中最大的距离,现在要求所有路径中最小的跳跃距离,就是求所有路径中最大距离的最小值。

思路

POJ-1979 Heavy Transportation这一题很像,建图后都是最大生成树。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
#include<cmath>
const int MAXN = 205, INF = 0x3f3f3f3f;
using namespace std;
struct node{
int from, to;
double val;
}E[MAXN*MAXN];
int mp[MAXN][3], F[MAXN];

double dis(int x1, int y1, int x2, int y2){
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}

bool cmp(node a, node b){
return a.val < b.val;
}

int Find(int x){
if(F[x] == x) return x;
else return Find(F[x]);
}

bool Merge(int x, int y){
x = Find(x);
y = Find(y);
if(x == y) return false;
else F[x] = y;
return true;
}

int main(){
int n, x, y;
int cas = 1;
while(scanf("%d", &n) && n){
for(int i = 0; i < n; i++) F[i] = i;
int k = 0;
for(int i = 0; i < n; i++){
scanf("%d %d", &mp[i][0], &mp[i][1]);
for(int j = 0; j < i; j++){
E[k].from = i;
E[k].to = j;
E[k++].val = dis(mp[i][0], mp[i][1], mp[j][0], mp[j][1]);
}
}
sort(E, E+k, cmp);
double res = 0;
for(int i = 0; i < k; i++){
if(Merge(E[i].from, E[i].to)){
if(Find(0) == Find(1)){
res = E[i].val;
break;
}
}
}
printf("Scenario #%d\n", cas++);
printf("Frog Distance = %.3f\n\n", res);
}
}

/*

2
0 0
3 4

3
17 4
19 4
18 5

0

*/